package edu.guet.server.service;

import edu.guet.server.mapper.CommentMapper;
import edu.guet.server.mapper.UserMapper;
import edu.guet.server.model.Comment;

import edu.guet.server.model.User;
import edu.guet.server.utils.ResultAPI;
import edu.guet.server.utils.SensitiveUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;


@Service
@Transactional
public class CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;

    //根据评论区id获取评论
    public ResultAPI selectComment(int post_id)
    {
        // 根据评论区id查询所有评论
        List<Comment> commentList = commentMapper.selectComment(post_id);
        if (commentList!=null){
            for(Comment a: commentList){
                //根据用户id获取用户名
                User user = userMapper.getUserById(a.getUser_id());
                a.setUsername(user.getUsername());
                a.setUser(user);
            }
            return ResultAPI.success(commentList,"评论查询成功！");
        }
        else
            return ResultAPI.success(null,"评论区无评论！");
    }

    //根据时间排序评论
    public ResultAPI sortComment(int post_id)
    {
        // 根据评论区id获取所有评论
        List<Comment> commentList = commentMapper.sortComment(post_id);
        if (commentList!=null){
            for(Comment a: commentList ){
                //根据用户id获取用户名
                User user = userMapper.getUserById(a.getUser_id());
                a.setUsername(user.getUsername());
                a.setUser(user);
            }
            return ResultAPI.success(commentList,"评论查询成功！");
        }
        else
            return ResultAPI.success(null,"评论区无评论！");
    }

    //发布评论
    public ResultAPI addComment(Comment comment,int post_id)
    {
        if(!SensitiveUtils.isCompliant(SensitiveUtils.SensitiveUtils(comment.getContent())))
        {
            return ResultAPI.error("评论内包含敏感词，请修改");
        }

        comment.setPost_id(post_id);

        int res=commentMapper.addComment(comment);
        if(res>0)
        {
            comment.setUser(userMapper.getUserById(comment.getUser_id()));
            comment.setComment_id(commentMapper.firstCommentByTime(post_id).getComment_id());
            comment.setUpdate_time(commentMapper.firstCommentByTime(post_id).getUpdate_time());
            return ResultAPI.success(comment,"评论发布成功,发布者是:");
        }
        else
            return ResultAPI.error("评论发布失败");
    }

    //修改评论
    public ResultAPI changeComment(Comment comment,int post_id)
    {
        if(!SensitiveUtils.isCompliant(SensitiveUtils.SensitiveUtils(comment.getContent())))
        {
            return ResultAPI.error("评论内包含敏感词，请修改");
        }
        comment.setPost_id(post_id);
        int res=commentMapper.changeComment(comment);
        if(res>0){
            Comment newcomment=commentMapper.findCommentById(comment.getComment_id());
            comment.setUpdate_time(commentMapper.firstCommentByTime(post_id).getUpdate_time());
            return ResultAPI.success(newcomment,"评论修改成功！");
        }
        else
            return ResultAPI.success(null,"该评论不存在！");
    }

    //删除评论
    public ResultAPI deleteComment(Comment comment)
    {
        int res=commentMapper.deleteComment(comment.getComment_id());
        if(res>0){
            return ResultAPI.success(null,"评论删除成功！");
        }
        else
            return ResultAPI.success(null,"该评论不存在！");
    }

    public List<Comment> getMostComment(int courseId) {
       return commentMapper.getNewComment(courseId);}
}
